package com.xinlu.user.action;

import java.io.IOException;

import com.xinlu.common.game.bo.ConstantLstBo;
import com.xinlu.derek.common.BaseAction;
import com.xinlu.derek.common.BaseForm;
import com.xinlu.persistence.Users;
import com.xinlu.persistence.Xxtx;
import com.xinlu.user.bo.XxtxBo;
import com.xinlu.user.bo.YhxxBo;
import com.xinlu.user.form.YhxxForm;

public class YhxxAction extends BaseAction {

	YhxxForm fr = new YhxxForm();
	private YhxxBo yhxxBo;
	private XxtxBo xxtxBo;

	public void setXxtxBo(XxtxBo xxtxBo) {
		this.xxtxBo = xxtxBo;
	}

	public void setFr(YhxxForm fr) {
		this.fr = fr;
	}

	public void setYhxxBo(YhxxBo yhxxBo) {
		this.yhxxBo = yhxxBo;
	}

	@Override
	public String doExecute() throws Exception {
		return null;
	}

	@Override
	public void formInit() {
	}

	@Override
	public BaseForm getModel() {
		return fr;
	}

	/**
	 * 加载页面
	 * 
	 * @return
	 */
	public String loadPage() {

		Users crtUser = (Users) getSession().getAttribute("userinfo");

		fr.setXxLeiXings(ConstantLstBo.getXxLeiXings());
		fr.setXxLeiXing("0");

		fr.setPageObject(yhxxBo.loadYhxx(crtUser.getUsername(), fr.getXxLeiXing(), fr.getPn()));

		return "yhxx";
	}

	/**
	 * 加载用户消息
	 * 
	 * @return
	 */
	public String loadYhxx() {

		Users crtUser = (Users) getSession().getAttribute("userinfo");

		fr.setPageObject(yhxxBo.loadYhxx(crtUser.getUsername(), fr.getXxLeiXing(), fr.getPn()));
		fr.setXxLeiXings(ConstantLstBo.getXxLeiXings());

		return "yhxx";
	}

	/**
	 * 更改消息为已读
	 * 
	 * @return
	 */
	public String changeHasRead() {

		yhxxBo.ChangeHasRead(Integer.parseInt(fr.getXxid()));

		Users crtUser = (Users) getSession().getAttribute("userinfo");
		fr.setPageObject(yhxxBo.loadYhxx(crtUser.getUsername(), fr.getXxLeiXing(), fr.getPn()));
		fr.setXxLeiXings(ConstantLstBo.getXxLeiXings());

		return "yhxx";
	}

	/**
	 * 加载未读信息
	 * 
	 * @return
	 * @throws IOException
	 */
	public String loadMsgCount() throws IOException {

		Users crtUser = (Users) getSession().getAttribute("userinfo");
		if (crtUser == null) {

		}
		
		Xxtx xxtx = (Xxtx) getSession().getAttribute("xxtx");

		int count = yhxxBo.getWeiDuXiaoXi(crtUser);
		int alertCount = yhxxBo.getWeiDuXiaoXi(crtUser, xxtx.getCktx(), xxtx.getQktx(), xxtx.getPjtx(), xxtx.getFdtx());

		// 保存上一次
		int lastMsgCount = 0;
		if (getSession().getAttribute("lastMsgCount") != null) {
			// 表示上一次为空
			lastMsgCount = ((Integer) getSession().getAttribute("lastMsgCount")).intValue();
		}

		getResponse().getWriter().write((lastMsgCount < alertCount ? true : false) + ";" + count);
		getSession().setAttribute("lastMsgCount", alertCount);

		return null;
	}
}
